package cn.gjxblog.robot;


import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * @Author guojiaxun
 * @Date 2019/9/26 10:31
 */
public class ImageUtils {
    /**
     * 将图片改变成像素数据，同时获取搜索图片时的图片相关参数
     * @param bufferedImage 要转换的成像素数据的图片
     * @return {@link RgbImageComparerBean}
     */
    public static RgbImageComparerBean getPX(BufferedImage bufferedImage) {
        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();
        int minx = bufferedImage.getMinX();
        int miny = bufferedImage.getMinY();

        RgbImageComparerBean rgb = new RgbImageComparerBean();
        int colorArray[][] = new int[width][height];
        for (int i = minx; i < width; i++) {
            for (int j = miny; j < height; j++) {
                colorArray[i][j] = bufferedImage.getRGB(i, j);
            }
        }
        rgb.setColorArray(colorArray);
        return rgb;
    }

    public static boolean colorCompare(int pxSource,int pxSearch,int sim){
            if(sim == 0) {
                return pxSource == pxSearch;
            }else {
                Color sourceRgb = ColorUtil.intToColor(pxSource);
                Color searchRgb = ColorUtil.intToColor(pxSearch);
                return colorCompare(sourceRgb, searchRgb, sim);
            }
    }

    public  static boolean colorCompare(Color color1,Color color2,int sim){
        if(Math.abs(color1.getRed()-color2.getRed())<=sim && Math.abs(color1.getGreen()-color2.getGreen())<=sim && Math.abs(color1.getBlue()-color2.getBlue())<=sim){
            return true;
        }else{
            return false;
        }
    }
}
